<template>
  <div id="addUser">
    <heads></heads>
    <main>
      <el-form :rules="rules" :model="userInfo" ref="userInfo" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名称" prop="name">
          <el-input v-model="userInfo.name"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="userInfo.phone"></el-input>
        </el-form-item>
        <el-form-item label="用户住址" prop="address">
          <el-select v-model="userInfo.address" multiple filterable remote reserve-keyword placeholder="请输入关键词" :remote-method="remoteMethod" :loading="loading">
            <el-option v-for="item in options" :key="item.id" :label="item.cityname" :value="item.cityname"> {{ item.cityname }}</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上传头像" prop="resource">
          <el-upload action="http://127.0.0.1:8000/admin/Upload/addFile" ref="preUpload" list-type="picture-card" :auto-upload="true" :on-success="uploadSet">
            <i slot="default" class="el-icon-plus"></i>
            <div slot="file" slot-scope="{ file }">
              <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
              <span class="el-upload-list__item-actions">
                <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                  <i class="el-icon-zoom-in"></i>
                </span>
              </span>
            </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="个人简介" prop="desc">
          <el-input type="textarea" v-model="userInfo.textInfo"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="addUser">立即创建</el-button>
          <el-button v-on:click="resets">重置</el-button>
        </el-form-item>
      </el-form>
    </main>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import heads from "@/components/adminHead.vue";

import { addNewUser, selectAddress, deleteFile } from "@/api/getPostApi";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {
    heads,
  },
  data() {
    //这里存放数据
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      options: [],
      userInfo: {
        name: "",
        phone: "",
        address: "",
        fileName: "",
        textInfo: "",
      },
      rules: {
        name: [{ required: true, message: "请输入用户名称", trigger: "blur" }],
        phone: [{ required: true, message: "请输入用户电话", trigger: "blur" }],
        address: [{ required: true, message: "请输入用户住址", trigger: "blur" }],
      },
      loading: false,
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    remoteMethod(val) {
      this.loading = true;
      selectAddress(val).then((response) => {
        this.options = response.data;
        this.loading = false;
      });
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    uploadSet(val) {
      this.userInfo.fileName = val;
    },
    addUser() {
      this.$refs.userInfo.validate((vaild) => {
        if (!vaild) {
          return false;
        }

        return true;
      });
      if (!this.userInfo.name) {
        return false;
      }

      if (!this.userInfo.phone) {
        return false;
      }

      if (!this.userInfo.textInfo) {
        return false;
      }

      addNewUser(this.userInfo.name, this.userInfo.address[0], this.userInfo.fileName, this.userInfo.phone, this.userInfo.textInfo).then((response) => {
        console.log(response);
        if (response.data.status === 200) {
          this.$message({
            message: "恭喜你，成功添加",
            type: "success",
          });
        } else {
          this.$message.error("错了哦，添加失败");
        }
      });
      this.resets();
      return true;
    },
    resets() {
      this.userInfo.name = "";
      this.userInfo.address = "";
      this.userInfo.phone = "";
      this.userInfo.textInfo = "";
      this.$refs.preUpload.uploadFiles.forEach((val, index) => {
        if (val.response === this.userInfo.fileName) {
          this.$refs.preUpload.uploadFiles.splice(index, 1);
        }
      });
      this.userInfo.fileName = "";
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {
    if (this.userInfo.fileName) {
      deleteFile(this.userInfo.fileName).then((response) => {
        console.log(response);
      });
    }
  }, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="scss" scoped>
//@import url(); 引入公共css类
#addUser {
  main {
    width: 50%;
    margin: 1rem auto;
  }
}
</style>
